﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.finance
{
    public partial class 原料入库凭证 : System.Web.UI.Page
    {
        protected string Notice = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Date1.Text = DateTime.Today.ToLongDateString();
                string list = Request.QueryString["list"];
                string idstr = Request.QueryString["id"];
                int id;
                if (list != null && list != "")
                {
                    this.Label_List.Text = list;
                    string id1 = list.Split(",".ToCharArray())[0];


                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText =
@"declare @c1 int,@c2 int,@c3 int,@c4 int,@原料ID int,@供货ID int,@支数 int,@磅重 numeric(18,3);
select @c1=COUNT(distinct 原料ID),@c2=COUNT(distinct 供货ID),@c3=count(distinct 分厂) from 原料到发记录 where 状态>1 and 入库单ID=0 and ID in (" + list + @");
if @c1!=1 or @c2!=1 or @c3!=1 begin select 0,0,0,0;return;end;
select @原料ID=原料ID,@供货ID=供货ID from 原料到发记录 where id =" + id1 +
@";select @磅重=SUM(磅重),@支数=SUM(总支数) from 原料到发记录 where id in(" + list + @");
select @原料ID,@供货ID,@磅重,@支数;";
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    if (Reader.Read())
                    {
                        this.LockSum.Text = "true";
                        if ((int)Reader[0] == 0)
                        {
                            this.Notice = "·请选择相同原料类别、供货商、分厂的未入库记录填单。";
                            this.Button_Save.Visible = false;
                            this.IsReadonly.Text = "true";
                        }
                        else
                        {
                            this.LeiBie.Text = ((int)Reader[0]).ToString();
                            this.LeiBie.Enabled = false;
                            this.GongHuo.Text = ((int)Reader[1]).ToString();
                            this.GongHuo.Enabled = false;
                            this.ZZL.Text = ((decimal)Reader[2]).ToString();
                            this.ZZS.Text = ((int)Reader[3]).ToString();
                            this.ZSList.Value = this.ZZS.Text;
                            this.ZLList.Value = this.ZZL.Text;
                        }
                    }
                    else
                    {
                        this.Notice = "·查询错误";
                        this.Button_Save.Visible = false;
                        this.IsReadonly.Text = "true";
                    }
                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();

                }
                else if (idstr != null && int.TryParse(idstr,out id))
                {
                    this.Label_ID.Text = idstr;
                    this.Button_Save.Visible = false;
                    this.IsReadonly.Text = "true";

                    SqlConnection Conn = new SqlConnection();
                    Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                    Conn.Open();
                    SqlCommand Cmd = new SqlCommand();
                    Cmd.Connection = Conn;
                    Cmd.CommandText = @"select 入出,单据号,原料ID,单位ID,日期,重量,支数,单价,备注 from 原料入出库 where id="+idstr +" or fid="+idstr;
                    Cmd.CommandType = CommandType.Text;
                    SqlDataReader Reader = Cmd.ExecuteReader();
                    decimal zzl = 0;
                    int zzs = 0;

                    if (Reader.Read())
                    {
                        if ((byte)Reader["入出"] == 0 || (byte)Reader["入出"] == 2)
                        {
                            this.DanHao.Text = ((int)Reader["单据号"]).ToString();
                            this.LeiBie.Text = ((int)Reader["原料ID"]).ToString();
                            this.GongHuo.Text = ((int)Reader["单位ID"]).ToString();
                            this.Date1.Text = ((DateTime)Reader["日期"]).ToLongDateString();

                            zzs += (int)Reader["支数"];
                            zzl += (decimal)Reader["重量"];
                            this.ZSList.Value += ((int)Reader["支数"]).ToString() + "|";
                            this.ZLList.Value += Convert.ToDouble((decimal)Reader["重量"]).ToString() + "|";
                            this.DJList.Value += Convert.ToDouble((decimal)Reader["单价"]).ToString() + "|";

                            if (!Convert.IsDBNull(Reader["备注"]))
                            {
                                this.BeiZhu.Value = (string)Reader["备注"];
                            }

                            while (Reader.Read())
                            {
                                zzs += (int)Reader["支数"];
                                zzl += (decimal)Reader["重量"];
                                this.ZSList.Value += ((int)Reader["支数"]).ToString() + "|";
                                this.ZLList.Value += Convert.ToDouble((decimal)Reader["重量"]).ToString() + "|";
                                this.DJList.Value += Convert.ToDouble((decimal)Reader["单价"]).ToString() + "|";
                            }

                            this.ZZS.Text = zzs.ToString();
                            this.ZZL.Text = zzl.ToString();

                        }
                        else
                        {
                            this.Notice = "·读取错误";
                        }
                    }
                    else
                    {
                        this.Notice = "·入库单不存在";
                    }
                    Reader.Close();
                    Conn.Close();
                    Reader.Dispose();
                    Cmd.Dispose();
                    Conn.Dispose();
                }
                
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            if (this.ZSList.Value.Replace("|", "") == "")
            {
                this.Notice = "·请录入数量、重量、单价。";
                return;
            }
            DateTime d;
            int dh;
            if (!DateTime.TryParse(this.Date1.Text, out d))
            {
                this.Notice = "·日期录入有误。";
                return;
            }
            if (!int.TryParse(this.DanHao.Text, out dh))
            {
                this.Notice = "·单号录入有误。";
                return;
            }

            string[] zslst = this.ZSList.Value.Split(new char[] { '|' });
            string[] zllst = this.ZLList.Value.Split(new char[] { '|' });
            string[] djlst = this.DJList.Value.Split(new char[] { '|' });

            decimal zzl=0;
            int zzs=0;
            int i;
            string sql = "";
            int jj = 0;
            if (this.DanWei.Items[1].Selected)
            {
                jj = 1;
            }

            for (i = 0; i < zslst.Length; i++)
            {
                zzl += decimal.Parse(zllst[i]);
                zzs += int.Parse(zslst[i]);
                sql += "set @支数=" + zslst[i] + ";";
                sql += "set @重量=" + zllst[i] + ";";
                sql += "set @单价=" + djlst[i] + ";";
                sql += "set @结存重量=@结存重量+@重量;";
                sql += "set @结存支数=@结存支数+@支数;";
                if (this.DanWei.SelectedIndex == 0)
                {
                    sql += "set @结存金额=@结存金额+@重量*@单价;";
                }
                else
                {
                    sql += "set @结存金额=@结存金额+@支数*@单价;";
                }

                sql += "insert into 原料入出库(FID,单据号,原料ID,单位ID,入出,日期,重量,支数,单价,结存重量,结存支数,结存金额,计价方式,备注)"
                    + "values(@FID,@单据号,@原料ID,@单位ID,@入出,@日期,@重量,@支数,@单价,@结存重量,@结存支数,@结存金额,@计价方式,@备注);";
                if (i == 0)
                {
                    sql += "set @FID=@@IDENTITY;";
                    if (this.Label_List.Text != "")
                    {
                        sql += "update 原料到发记录 set 入库单ID=@FID where id in(" + this.Label_List.Text + ");";
                    }
                }
            }
 
            if (this.Label_List.Text != "")
            {
                if (zzs != int.Parse(this.ZZS.Text))
                {
                    this.Notice = "·录入的入库总支数与到货总支数不符。";
                    return;
                }
                if (zzl != decimal.Parse(this.ZZL.Text))
                {
                    this.Notice = "·录入的入库总重量与到货总重量不符。";
                    return;
                }
            }
            sql = "declare @FID int,@单据号 int,@原料ID int,@单位ID int,@入出 tinyint,@日期 date"
                + ",@重量 numeric(18,4),@支数 int,@单价 numeric(27,10),@计价方式 tinyint,@备注 nvarchar(50);"
                + "declare @结存重量 numeric(18,4),@结存支数 int,@结存金额 numeric(18,3);"
                + "set @FID=0"
                + "set @单据号=" + dh
                + "set @原料ID=" + this.LeiBie.Text
                + "set @单位ID=" + this.GongHuo.Text
                + "set @入出=0"
                + "set @日期='" + d.ToShortDateString() + "'"
                + "set @计价方式=" + jj
                + "set @备注='" + this.BeiZhu.Value.Replace("'", "''") + "'"
                + "select top 1 @结存重量=结存重量,@结存支数=结存支数,@结存金额=结存金额 from 原料入出库 where 原料ID=@原料ID order by ID desc;"
                + "select @结存重量=ISNULL(@结存重量,0),@结存支数=ISNULL(@结存支数,0),@结存金额=ISNULL(@结存金额,0);"
                + sql;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = sql;
            Cmd.ExecuteNonQuery();
            Conn.Close();
            Cmd.Dispose();
            Conn.Dispose();
            this.Button_Save.Visible = false;
            this.IsReadonly.Text = "true";
            this.Notice = "·保存成功。";
        }
    }
}
